package ThreadPool.chapter2;

import java.nio.channels.NonReadableChannelException;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;

/**
 * @param
 * @author oyz
 * @version 1.0.4
 * @description:TODO
 * @date 2022/12/20 21:11
 */
public class Caller implements Runnable{

    private Guard guard;

    public Caller(Guard guard){
        this.guard = guard;
    }

    private AtomicLong successFulCalls = new AtomicLong();

    private AtomicBoolean flag = new AtomicBoolean(false);


    @Override
    public void run() {
        //定时器,等待2.5秒后执行
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                flag.set(true);
            }
        },2500);
        while(!flag.get()){
            successFulCalls.incrementAndGet();
            guard.method();

        }
        System.out.println("一共执行了"+successFulCalls.get());
    }
}
